let Koa = require('koa');
let wechat = require('./wechat/g');
let config = require('./wechat/config');
const path = require('path');
const bodyParser = require('koa-bodyparser');
let ejs = require('ejs');
let tpl = require('./wechat/movie/tpl');
let search = require('./wechat/search/tpl');
let Wechat = require('./wechat/wechat');
let $ = require('jquery')
const static = require('koa-static');


let app = new Koa();
app.use(bodyParser());
// 配置静态web服务的中间件
app.use(static(
  path.join( __dirname,'./material')
));
app.use(function *(next) {
  console.log(this.query)
  if (this.url.indexOf('/movie') != -1) {
    if (this.url.indexOf('MP_verify_GrETbKHAWegtcnrs.txt') != -1) {
      this.body = 'GrETbKHAWegtcnrs'
    } else {
      let wx = new Wechat(config.wechat);
      let ticketData =yield wx.fetchTicket();
      let url = this.href.split('#')[0];
      let params = tpl.sign(ticketData, url);
      this.body = ejs.render(tpl.movie, params);
    }
    return next
  }
  if (this.url.indexOf('/search') != -1) {
    let title = this.query.title;
    console.log(this.url)
    console.log(title)
    let content = {
      title
    }
    this.body = ejs.render(search.search, content);
    return next
  }
  yield next
});
app.use(wechat(config.wechat));
app.listen(80);

console.log("Lsssss");
